#!/bin/bash

source config/init_proj.sh

interfaceWare=${LINK_INTERFACE}
projectName=${PROJECT}

configPath=Project/${projectName}/config.cmake
interfaceFile=platform/${PLATFORM}/${PLATFORM:0:7}x.cfg

function halt(){
    openocd -f $interfaceWare -f $interfaceFile -c init -c halt -c exit
}

function reset(){
    openocd -f $interfaceWare -f $interfaceFile -c init -c reset -c halt -c reset -c exit
}

function startup(){
    start openocd -f $interfaceWare -f $interfaceFile
}

function download(){
    openocd -f $interfaceWare -f $interfaceFile \
            -c "program out/$projectName/$projectName.bin 0x8000000 verify reset exit"
}

function unlock(){
    series=`echo $interfaceFile | cut -d "." -f 1`
    openocd -f $interfaceWare \
            -f $interfaceFile \
            -c "init" \
            -c "halt" \
            -c "$series unlock 0" \
            -c "reset halt" \
            -c "exit"
}

while getopts "draus" arg
do
  case $arg in
    d)
      download
      ;;
    r)
      reset
      ;;
    a)
      halt
      ;;
    s)
      startup
      ;;
    u)
      unlock
      ;;
    *)
      echo read scripts
      ;;
  esac
done